import json
import requests
from bs4 import BeautifulSoup
import time
import os
"""
热榜：
- 百度 https://top.baidu.com/board?tab=realtime
- 
结构：
1.获取热搜的时间戳，之后的命名都是用这个时间戳作为路径。
2.模块设计：
"""
# 获得当前时间戳
def get_timestamp():
    # 输出格式为年月日时分秒
    time_stamp = time.strftime("%Y_%m_%d-%H_%M_%S", time.localtime())
    return time_stamp

def url_data(url):
    "return: bool, response_text"
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0"
    }
    response = requests.get(url, headers=headers)
    response_status = response.status_code==200

    if response_status:
        # print("请求成功！")
        return response_status, response.text
    else:
        print(f"请求失败，状态码: {response.status_code}")
        return response_status, None

def weibo(data_path):
    time_stamp = time.strftime("%Y_%m_%d-%H_%M_%S", time.localtime())
    """保存路径：data_path/time_stamp"""
    url = "https://tophub.today/n/KqndgxeLl9" 
    response_status, response_text = url_data(url)

    if response_status==True:
        soup = BeautifulSoup(response_text, "lxml")
        search_results = soup.find_all('table', class_='table')[0].find_all('td', class_='al')
        news_dict = {}
        title_list = []
        for ind, search in enumerate(search_results):
            title = search.find('a').text
            news_dict[f"{ind}"] = {"title": title}
            title_list.append(title)

        os.makedirs(f'{data_path}/{time_stamp}/weibo', exist_ok=True)
        with open(f'{data_path}/{time_stamp}/weibo/hotsearch.json', "w") as f:
            json.dump(news_dict, fp=f, ensure_ascii=False)
        return True, news_dict, title_list
    else:
        print("无法获取热搜数据。")
        return False, None, None
    

def baidu(response_text, time_stamp, data_path):
    pass



if __name__ == '__main__':
    data_path = "db"
    response_status, news_dict, title_list = weibo(data_path)
    print(news_dict)